package com.yuexun.dhdoctor.module.survey.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseEntity;
import com.yuexun.dhdoctor.baselistener.ISelAndSearchCallBack;
import com.yuexun.dhdoctor.baselistener.OnItemPartClickListener;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.survey.adapter.SurveyListAdapter;
import com.yuexun.dhdoctor.module.survey.bean.SurveyDateBean;
import com.yuexun.dhdoctor.module.survey.model.impl.SurveyModel;
import com.yuexun.dhdoctor.module.survey.presenter.impl.SurveyListPresenter;
import com.yuexun.dhdoctor.module.survey.ui.Act_Survey_Start;
import com.yuexun.dhdoctor.module.survey.ui.view.ISurveyListView;
import com.yuexun.dhdoctor.widget.EmptyLayout;
import com.yuexun.dhdoctor.widget.PullToRefresh.FooterLoadingLayout;
import com.yuexun.dhdoctor.widget.PullToRefresh.PullToRefreshBase;
import com.yuexun.dhdoctor.widget.PullToRefresh.PullToRefreshList;

import org.kymjs.kjframe.ui.KJFragment;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by wangt on 2017/8/2
 */

public class Frag_Survey_DraftBox extends KJFragment implements ISelAndSearchCallBack, ISurveyListView {

    @Bind(R.id.patient_list)
    PullToRefreshList patientList;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;

    private ListView mList;

    private SurveyListPresenter presenter;
    private SurveyListAdapter adapter;
    private List<SurveyDateBean> datas = new ArrayList<>();

    private int nowpage = 1;
    private int pagesize = 999;

    private String type = "-1";
    private String key = "";

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        View mMainView = inflater.inflate(R.layout.frag_exam_list, container, false);
        ButterKnife.bind(this, mMainView);
        return mMainView;
    }

    @Override
    protected void initData() {
        super.initData();
        presenter = new SurveyListPresenter(new SurveyModel(), this);
        listViewPreference();

        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
                nowpage = 1;
                ApiForGetList(type, key);
            }
        });
    }

    /**
     * 初始化ListView样式
     */
    private void listViewPreference() {
        mList = patientList.getRefreshView();
        mList.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
        patientList.setPullRefreshEnabled(false);
        patientList.setPullLoadEnabled(true);
        ((FooterLoadingLayout) patientList.getFooterLoadingLayout()).setNoMoreDataText("无更多数据");

        adapter = new SurveyListAdapter(mList, datas, R.layout.item_survey_draftbox);
        mList.setAdapter(adapter);

        patientList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                nowpage = nowpage + 1;
                ApiForGetList(type, key);
            }
        });

        adapter.setOnItemPartClickListener(new OnItemPartClickListener() {
            @Override
            public void OnClick(int type, int position) {
                Intent intent = new Intent(getActivity(), Act_Survey_Start.class);
                intent.putExtra("type", "get");
                intent.putExtra("surveyId", datas.get(position).getSurveyId());
                intent.putExtra("templateId", datas.get(position).getTemplateId());
                intent.putExtra("createName", datas.get(position).getCreateName());
                startActivity(intent);
            }
        });
    }

    /**
     * 网络请求获取列表数据
     */
    private void ApiForGetList(String type, String key) {
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuid", PreferenceHelper.readString(getActivity(), AppConfig.USER_INFO, AppConfig.SESSION_ID));
        params.put("inquireType", "0");//状态0草稿箱 1我提交的 2我收到的
        params.put("pageIndex", nowpage + "");
        params.put("pageSize", pagesize + "");
        params.put("inquireKey", key);
        if (!type.equals("-1")) {
            params.put("typeId", type);
        }
        presenter.getSurveyList(params);
    }

    @Override
    public void searchBack(String select, CharSequence s) {
        emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
        nowpage = 1;
        type = select;
        key = s.toString();
        ApiForGetList(type, key);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        nowpage = 1;
        ApiForGetList(type, key);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            type = "-1";
            key = "";
            nowpage = 1;
            ApiForGetList(type, key);
        }
    }

    @Override
    public void showSurveyList(BaseEntity<SurveyDateBean> bean) {
        if (patientList.isPullRefreshEnabled()) {
            patientList.onPullDownRefreshComplete();
        }


        if ("200".equals(bean.getStatus())) {
            if (bean.getDatas().size() == 0) {
                patientList.setHasMoreData(false);
            } else {
                if (patientList.isPullLoadEnabled()) {
                    patientList.onPullUpRefreshComplete();
                }
            }

            if (nowpage == 1) {
                datas.clear();
                datas = bean.getDatas();
            } else {
                datas.addAll(bean.getDatas());
            }
            adapter.refresh(datas);

        } else {
            ViewInject.toast(bean.getMessage());
        }

        if (datas.size() == 0) {
            emptyLayout.setErrorType(EmptyLayout.NODATA);
        } else {
            emptyLayout.dismiss();
        }
    }
}
